package com.demo.controller;

import com.demo.common.Result;
import com.demo.mapper.CourseMapper;
import com.demo.mapper.TeacherMapper;
import com.demo.pojo.entity.Course;
import com.demo.pojo.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程教师对应关系测试控制器
 */
@RestController
@RequestMapping("/course-teacher-test")
@CrossOrigin
public class CourseTeacherTestController {
    
    @Autowired
    private CourseMapper courseMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    /**
     * 测试课程和教师的对应关系
     */
    @GetMapping("/course-teacher-mapping")
    public Result<String> testCourseTeacherMapping() {
        try {
            List<Course> courses = courseMapper.selectList(null);
            StringBuilder result = new StringBuilder();
            result.append("课程-教师对应关系测试:\n");
            result.append("总课程数量: ").append(courses.size()).append("\n\n");
            
            for (Course course : courses) {
                result.append("课程: ").append(course.getCourseName()).append("\n");
                result.append("  课程编号: ").append(course.getCourseNo()).append("\n");
                result.append("  院系: ").append(course.getDept()).append("\n");
                result.append("  教师ID: ").append(course.getTeacherId()).append("\n");
                
                if (course.getTeacherId() != null) {
                    Teacher teacher = teacherMapper.selectById(course.getTeacherId());
                    if (teacher != null) {
                        result.append("  教师姓名: ").append(teacher.getTeName()).append("\n");
                        result.append("  教师工号: ").append(teacher.getTeNo()).append("\n");
                    } else {
                        result.append("  教师信息: 未找到对应教师\n");
                    }
                } else {
                    result.append("  教师信息: 未指定教师\n");
                }
                result.append("\n");
            }
            
            return Result.success(result.toString());
            
        } catch (Exception e) {
            return Result.error("测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试特定教师的课程
     */
    @GetMapping("/teacher-courses/{teacherId}")
    public Result<String> testTeacherCourses(@PathVariable Long teacherId) {
        try {
            Teacher teacher = teacherMapper.selectById(teacherId);
            if (teacher == null) {
                return Result.error("教师不存在");
            }
            
            List<Course> courses = courseMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Course>()
                    .eq("teacher_id", teacherId)
            );
            
            StringBuilder result = new StringBuilder();
            result.append("教师: ").append(teacher.getTeName()).append(" (工号: ").append(teacher.getTeNo()).append(")\n");
            result.append("教授课程数量: ").append(courses.size()).append("\n\n");
            
            for (Course course : courses) {
                result.append("课程: ").append(course.getCourseName()).append("\n");
                result.append("  课程编号: ").append(course.getCourseNo()).append("\n");
                result.append("  院系: ").append(course.getDept()).append("\n");
                result.append("  学分: ").append(course.getCredit()).append("\n");
                result.append("  学时: ").append(course.getPeriodTotal()).append("\n");
                result.append("  类型: ").append(course.getCourseType()).append("\n\n");
            }
            
            return Result.success(result.toString());
            
        } catch (Exception e) {
            return Result.error("测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试特定院系的课程
     */
    @GetMapping("/dept-courses/{dept}")
    public Result<String> testDeptCourses(@PathVariable String dept) {
        try {
            List<Course> courses = courseMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Course>()
                    .eq("dept", dept)
            );
            
            StringBuilder result = new StringBuilder();
            result.append("院系: ").append(dept).append("\n");
            result.append("课程数量: ").append(courses.size()).append("\n\n");
            
            for (Course course : courses) {
                result.append("课程: ").append(course.getCourseName()).append("\n");
                result.append("  教师ID: ").append(course.getTeacherId()).append("\n");
                
                if (course.getTeacherId() != null) {
                    Teacher teacher = teacherMapper.selectById(course.getTeacherId());
                    if (teacher != null) {
                        result.append("  教师姓名: ").append(teacher.getTeName()).append("\n");
                    }
                }
                result.append("\n");
            }
            
            return Result.success(result.toString());
            
        } catch (Exception e) {
            return Result.error("测试失败: " + e.getMessage());
        }
    }
}
























